<template>
  <div>
    <el-button @click="add">add</el-button>
    <el-button @click="del">del</el-button>
    <el-table
        :data="tableData"
        @expand-change="change"
        style="width: 100%">
      <el-table-column type="expand">
        <template #default="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="角色id">
              <span>{{ props.row.id }}</span>
            </el-form-item>
            <el-form-item label="菜单">
              <span>{{rowData[props.row.id]}}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>

      <el-table-column
          label="角色ID"
          prop="id">
      </el-table-column>
      <el-table-column
          label="角色名"
          prop="name">
      </el-table-column>
      <el-table-column
          label="中文名"
          prop="nameZh">
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {onMounted, ref} from "vue";
import {getRoleById, getRoles} from "../../api/role";
import request from "@/utils/request";
import {ElMessage} from "element-plus";
import {userDel} from "@/api/user";

export default {
  setup() {
    const add = () => {
      request({
        baseURL:`http://localhost:8000`,
        url: '/user/add',
        method: 'get',
      }).then(res=>{
        let{success,msg}= res
        if (success){
          ElMessage.success(msg)
        }else{
          ElMessage.error(msg)
        }

      }).catch(e=>{
        ElMessage.error(e)
      })
    }
    const del = () => {

      userDel().then(res=>{
        let{success,msg}= res
        if (success){
          ElMessage.success(msg)
        }else{
          ElMessage.error(msg)
        }
      }).catch(e=>{
        ElMessage.error(e)
      })
    }
    const tableData = ref()
    const rowData = ref([])
    // 编辑按钮
    const handleEdit = (index, row) => {
      console.log(row);
    }
    // 删除按钮
    const handleDelete = (index, row) => {
      console.log(index, row);
    }
    const change = (row, old) => {
      if (old.indexOf(row) !==-1){
       getRoleById(row.id).then(res=>{
          rowData.value[row.id] = res.data
       }).catch(e=>{console.log(e)})
      }else {
        rowData.value[row.id]=null
      }
    }
    onMounted(()=>{
      getRoles().then(res=>{
        tableData.value = res.data
      }).catch()
    })
    return{
      add,
      del,
      tableData,
      handleEdit,
      handleDelete,
      change,
      rowData
    }
  }
}
</script>

<style scope>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
